Cube = class()

function Cube:init(xTexOffs,yTexOffs,tex)
    self.xTexOffs = xTexOffs
    self.yTexOffs = yTexOffs
    self.x,self.y,self.z = 0,0,0
    self.xRot,self.yRot,self.zRot = 0,0,0
    self.t = Tesselator()
    self.tex = tex
end

function Cube:addBox(x0,y0,z0,l,h,w)
    self.vertices = {}
    self.polygons = {}
    
    local x1 = x0 + l
    local y1 = y0 + h
    local z1 = z0 + w
    
    --u
    local u1 = Vertex(x0,y0,z0,0,0)
    local u2 = Vertex(x1,y0,z0,0,8)
    local u3 = Vertex(x1,y1,z0,8,8)
    local u4 = Vertex(x0,y1,z0,8,0)
    
    local iu1 = Vertex(x0,y0,z0,0,0)
    local iu2 = Vertex(x1,y0,z0,0,8)
    local iu3 = Vertex(x1,y1,z0,8,8)
    local iu4 = Vertex(x0,y1,z0,8,0)
    
    local ju1 = Vertex(x0,y0,z0,0,0)
    local ju2 = Vertex(x1,y0,z0,0,8)
    local ju3 = Vertex(x1,y1,z0,8,8)
    local ju4 = Vertex(x0,y1,z0,8,0)
    --v
    local l1 = Vertex(x0,y0,z1,0,0)
    local l2 = Vertex(x1,y0,z1,0,8)
    local l3 = Vertex(x1,y1,z1,8,8)
    local l4 = Vertex(x0,y1,z1,8,0)
    
    local il1 = Vertex(x0,y0,z1,0,0)
    local il2 = Vertex(x1,y0,z1,0,8)
    local il3 = Vertex(x1,y1,z1,8,8)
    local il4 = Vertex(x0,y1,z1,8,0)
    
    local jl1 = Vertex(x0,y0,z1,0,0)
    local jl2 = Vertex(x1,y0,z1,0,8)
    local jl3 = Vertex(x1,y1,z1,8,8)
    local jl4 = Vertex(x0,y1,z1,8,0)
    
    self.vertices[1],self.vertices[2],self.vertices[3],self.vertices[4] = u1,u2,u3,u4
    self.vertices[5],self.vertices[6],self.vertices[7],self.vertices[8] = l1,l2,l3,l4
    
    self.polygons[1] = Polygon({ l2, u2, u3, l3 },self.xTexOffs+w+l,self.yTexOffs+w,self.xTexOffs+w+l+w,self.yTexOffs+w+h)                               --left
    self.polygons[2] = Polygon({ u1, l1, l4, u4 },self.xTexOffs,self.yTexOffs+w,self.xTexOffs+w,self.yTexOffs+w+h)                                               --right
    self.polygons[3] = Polygon({ il2, il1, iu1, iu2 },self.xTexOffs+w,self.yTexOffs,self.xTexOffs+w+l,self.yTexOffs+w)                                 --top
    self.polygons[4] = Polygon({ iu3, iu4, il4, il3 },self.xTexOffs+w+l,self.yTexOffs,self.xTexOffs+w+l+l,self.yTexOffs+w)                                 --bottom
    self.polygons[5] = Polygon({ ju2, ju1, ju4, ju3 },self.xTexOffs + w + l + w, self.yTexOffs + w, self.xTexOffs + w + l + w + l, self.yTexOffs + w + h)--back
    self.polygons[6] = Polygon({ jl1, jl2, jl3, jl4 },self.xTexOffs + w, self.yTexOffs + w, self.xTexOffs + w + l, self.yTexOffs + w + h)                          --front
end

function Cube:rebuild()
    self.t:begin()
    self.t:bindTex(self.tex)
    for k,v in pairs(self.polygons) do
        v:render(self.t)
    end
    self.t:setShader(shader("Project:TextureColorShader"))
    self.t:End()
end

function Cube:setPos(x,y,z)
    self.x,self.y,self.z = x,y,z
end

function Cube:render()
    local c = 57.29578
    pushMatrix()
    translate(self.x,self.y,self.z)
    rotate(self.zRot * c,0,0,1)
    rotate(self.yRot * c,0,1,0)
    rotate(self.xRot * c,1,0,0)
    self.t.mesh.shader.GlobalColor = world.globalColor
    self.t:render()
    popMatrix()
end